﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using aaaSoft.SkinEngine.SkinHelpers;

namespace aaaSoft.SkinEngine.MyControls
{
    class MyGroupBox:IMyControl
    {
        GroupBox gbBase;
        SkinEngine skinEng;

        public MyGroupBox(GroupBox gb, SkinEngine skinEng)
        {
            gbBase = gb;
            this.skinEng = skinEng;
        }

        public void StopControlSkin()
        {
            gbBase.Paint -= new PaintEventHandler(gbBase_Paint);
        }

        public void StartControlSkin()
        {
            gbBase.Paint += new PaintEventHandler(gbBase_Paint);
        }

        //文本的大小
        private SizeF TextSize
        {
            get
            {
                SizeF szText = gbBase.CreateGraphics().MeasureString(gbBase.Text, gbBase.Font);
                if (szText.Width > gbBase.Width - 2 * 7)
                {
                    szText.Width = gbBase.Width - 2 * 7;
                }
                return szText;
            }
        }
        //圆角宽度
        private int _RoundWidth = 8;
        public int RoundWidth
        {
            get
            {
                return _RoundWidth;
            }
            set
            {
                if (value > 0)
                {
                    _RoundWidth = value;
                    gbBase.Invalidate();
                }
            }
        }

        void gbBase_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(new SolidBrush(skinEng.BackColor), new Rectangle(new Point(0, 0), gbBase.Size));


            if (gbBase.Parent == null)
            {
                return;
            }

            Pen linePen = new Pen(ColorHelper.GetDeeperColor(skinEng.BackColor, 0.8F));
            //画连线
            int HalfRoundWidth = RoundWidth / 2;
            g.DrawLine(linePen, new Point(HalfRoundWidth, 7), new Point(gbBase.Width - HalfRoundWidth, 7));
            g.DrawLine(linePen, new Point(gbBase.Width - 1, 7 + HalfRoundWidth), new Point(gbBase.Width - 1, gbBase.Height - 1 - HalfRoundWidth));
            g.DrawLine(linePen, new Point(HalfRoundWidth, gbBase.Height - 1), new Point(gbBase.Width - 1 - HalfRoundWidth, gbBase.Height - 1));
            g.DrawLine(linePen, new Point(0, 7 + HalfRoundWidth), new Point(0, gbBase.Height - 1 - HalfRoundWidth));

            //画圆角
            g.DrawArc(linePen, new Rectangle(0, 7, RoundWidth, RoundWidth), -180, 90);
            g.DrawArc(linePen, new Rectangle(gbBase.Width - RoundWidth - 1, 7, RoundWidth, RoundWidth), -90, 90);
            g.DrawArc(linePen, new Rectangle(gbBase.Width - RoundWidth - 1, gbBase.Height - RoundWidth - 1, RoundWidth, RoundWidth), 0, 90);
            g.DrawArc(linePen, new Rectangle(0, gbBase.Height - RoundWidth - 1, RoundWidth, RoundWidth), 90, 90);

            //文字中间的一根线
            Color breakColor = skinEng.BackColor;
            g.DrawLine(new Pen(breakColor), 7, 7, 7 + TextSize.Width, 7);

            //画GroupBox文字
            SolidBrush backSB = new SolidBrush(gbBase.Parent.BackColor);
            backSB.Color = gbBase.ForeColor;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            sf.LineAlignment = StringAlignment.Center;
            sf.FormatFlags = StringFormatFlags.NoWrap;

            RectangleF rect = new RectangleF(new PointF(7, 2), TextSize);
            string displayText = SkinHelpers.GraphicHelper.GetAppropriateString(g, gbBase.Text, gbBase.Font, rect);
            g.DrawString(displayText, gbBase.Font, backSB, rect, sf);
        }

    }
}
